package com.eims.sls_app.fragment;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.VolleyError;
import bls.juejin.cn.R;
import com.eims.sls_app.dataaccess.DtMatch;
import com.eims.sls_app.dataaccess.DtMatch_Basketball;
import com.eims.sls_app.dataaccess.ImageViews;
import com.eims.sls_app.dataaccess.Lottery;
import com.eims.sls_app.dataaccess.utils.HttpUtils;
import com.eims.sls_app.protocol.MD5;
import com.eims.sls_app.protocol.RspBodyBaseBean;
import com.eims.sls_app.ui.Buy_DLT_Activit;
import com.eims.sls_app.ui.Buy_RX9_Activit;
import com.eims.sls_app.ui.Buy_SFC_Activity;
import com.eims.sls_app.ui.CustomerActivity;
import com.eims.sls_app.ui.FollowActivity;
import com.eims.sls_app.ui.LoginActivity;
import com.eims.sls_app.ui.LuckyCenterActivity;
import com.eims.sls_app.ui.MainActivity;
import com.eims.sls_app.ui.SelectNumberActivity;
import com.eims.sls_app.ui.SelectNumberActivityFC3D;
import com.eims.sls_app.ui.SelectNumberActivityPL3;
import com.eims.sls_app.ui.SelectNumberActivityPL5_QXC;
import com.eims.sls_app.ui.Select_11X5Activity;
import com.eims.sls_app.ui.Select_QlcActivity;
import com.eims.sls_app.ui.Select_SSCActivity;
import com.eims.sls_app.ui.Select_jclqActivity;
import com.eims.sls_app.ui.Select_jczqActivity;
import com.eims.sls_app.ui.Select_k3Activity;
import com.eims.sls_app.ui.SettingActivity;
import com.eims.sls_app.ui.adapter.CaceGvAdapter;
import com.eims.sls_app.ui.adapter.FBGridViewAdapter;
import com.eims.sls_app.ui.adapter.GridView_HallLotteryAdapter;
import com.eims.sls_app.ui.adapter.GuessNumGvAdapter;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.AppTools;
import com.eims.sls_app.utils.App;
import com.eims.sls_app.utils.ImageManager;
import com.eims.sls_app.utils.MyPushTask;
import com.eims.sls_app.utils.NetWork;
import com.eims.sls_app.utils.RequestUtil;
import com.eims.sls_app.view.MyGridView;
import com.eims.sls_app.view.MyToast;
import com.eims.sls_app.view.PullToRefreshView;
import com.eims.sls_app.view.PullToRefreshView.OnHeaderRefreshListener;
import com.eims.sls_app.viewpager.AutoLoopViewPager;
import com.eims.sls_app.viewpager.CirclePageIndicator;
import com.eims.sls_app.dataaccess.DtMatchBJDC;
import com.eims.sls_app.ui.Select_bjdcActivity;

/**
 ** 大厅
 */
@SuppressLint({ "NewApi", "DefaultLocale" })
public class HallNewFragment extends Fragment implements OnClickListener,
		OnHeaderRefreshListener {
	public int[] pic = {R.drawable.ic_index_cele,R.drawable.ic_index_chat,R.drawable.library,R.drawable.indexcenter};
	public String[] title = {"比分直播","必发","赛前爆料","合买中心"};
	public String[] subtitle ={"足球篮球即时呈现","交易额Top5","比赛胜负，数据有话说","众人拾柴 火焰高"};
	public String[] lottery_football = {"混合","猜比分","进球数","胜平负"};
	public String[] lottery_basketball = {"混合","胜负","胜分差","大小分"};
	public int[] lottery_pic = {R.drawable.log_lottery_115,R.drawable.log_lottery_11yun,R.drawable.log_lottery_ssc,R.drawable.log_lottery_k3,R.drawable.log_lottery_dlt,R.drawable.log_zq,R.drawable.log_zq};
	public String[] lottery_title = {"广东11选5","十一运夺金","重庆时时彩","江苏快3","大乐透","胜负彩","任九场"};
	public CaceGvAdapter caceGvAdapter;
	public View v;
	private ImageView mIvIconService;
	private LinearLayout ll_zq_lottery_football,ll_lq_lottery_basketball;
	private SharedPreferences settings;// 保存购彩大厅彩种id位置
	private static final String TAG = "HallFragment";
	public static String Message;
	private GridView_HallLotteryAdapter gvAdapter;
	private FBGridViewAdapter fGridViewAdapter,bGridViewAdapter;
	private GuessNumGvAdapter guessNumGvAdapter;
	private PullToRefreshView mPullToRefreshView;
	private GridView gv_hall_lbji,gv_ic_lottery_football,gv_ic_lottery_basketball;
	private MyGridView gv_GuessNum;
	// private Button btn_luck;
	public static List<Lottery> listLottery;
	public static boolean refreType = true;
	private Context context;

	private Intent intent;
	private MainActivity activity;
	private List<RequestUtil> requestUtils;
	private List<Integer> menuList = new ArrayList<Integer>();
	private AutoLoopViewPager loopView;
	private ArrayList<ImageViews> imageviews;// 广告条数据
	private CirclePageIndicator indy;
	private String auth;
	private String opt, info, time, imei, crc;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.i(TAG, "onCreate");
		//setHasOptionsMenu(true);
		
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.i(TAG, "onCreateView");
		if (v == null) {
			v = inflater.inflate(R.layout.activity_new_hall, container, false);
		}
		init();
		findView(v);
		setListener();
		
		
		Log.e("1", "listLottery"+listLottery);
		return v;
	}

	/**
	 * 初始化
	 */
	private void init() {
		context = getActivity();
		listLottery = getData();
		//getLotteryData();
		activity = (MainActivity) getActivity();
	}

	private void findView(View v) {
		ll_zq_lottery_football = (LinearLayout)v.findViewById(R.id.ll_zq_lottery_football);
		ll_lq_lottery_basketball = (LinearLayout)v.findViewById(R.id.ll_lq_lottery_basketball);
		gv_ic_lottery_football = (GridView)v.findViewById(R.id.gv_ic_lottery_football);
		gv_ic_lottery_basketball = (GridView)v.findViewById(R.id.gv_ic_lottery_basketball);
		gv_GuessNum = (MyGridView)v.findViewById(R.id.gv_GuessNum);
		mIvIconService = (ImageView) v.findViewById(R.id.iv_icon_service);
		imageviews = new ArrayList<ImageViews>();
		gv_hall_lbji = (GridView) v.findViewById(R.id.gv_hall_lnji);
		caceGvAdapter = new CaceGvAdapter(getActivity(), pic, title, subtitle);
		gv_hall_lbji.setAdapter(caceGvAdapter);
		fGridViewAdapter = new FBGridViewAdapter(getActivity(),lottery_football);
		gv_ic_lottery_football.setAdapter(fGridViewAdapter);
		bGridViewAdapter = new FBGridViewAdapter(getActivity(), lottery_basketball);
		gv_ic_lottery_basketball.setAdapter(bGridViewAdapter);
		guessNumGvAdapter = new GuessNumGvAdapter(getActivity(),lottery_pic,lottery_title);
		gv_GuessNum.setAdapter(guessNumGvAdapter);
		
		mPullToRefreshView = (PullToRefreshView) v.findViewById(R.id.mPullToRefreshView);

		/*if (listLottery != null && listLottery.size() > 0) {
			gvAdapter = new GridView_HallLotteryAdapter(context, listLottery);
		} else {
			gvAdapter = new GridView_HallLotteryAdapter(context, getData());
		}*/
		//gv_hall_lottry.setAdapter(gvAdapter);
		//gv_hall_lottry.setFocusable(false);

		mPullToRefreshView.setOnHeaderRefreshListener(this);

		// btn_luck = (Button) v.findViewById(R.id.btn_luckyBtn);
		loopView = (AutoLoopViewPager) v.findViewById(R.id.autoLoop);
		indy = (CirclePageIndicator) v.findViewById(R.id.indy);

		/** 要更改的 **/
		menuList.add(R.drawable.menu_refresh_change);
		menuList.add(R.drawable.menu_setting_change);
		menuList.add(R.drawable.menu_changeuser_change);
		menuList.add(R.drawable.menu_exit_change);

		if (NetWork.isConnect(context)) {
			if (null == requestUtils)
				requestUtils = new ArrayList<RequestUtil>();
			getLotteryData();// 获取彩种信息
			doAutoLogin();// 自动登录
			LoadImageview();
		} else {
			MyToast.getToast(context, "网络连接异常，请检查网络").show();
			MainActivity.ll_main.setVisibility(View.GONE);
		}
	}

	private void LoadImageview() {
		imageviews.clear();
		MyAsynTask3 asynTask3 = new MyAsynTask3();
		asynTask3.execute();
	}

	/**
	 * 绑定监听
	 */
	private void setListener() {
		// btn_luck.setOnClickListener(this);
		mIvIconService.setOnClickListener(this);
		ll_zq_lottery_football.setOnClickListener(this);
		ll_lq_lottery_basketball.setOnClickListener(this);
		gv_hall_lbji.setOnItemClickListener(listenerlbji);
		gv_GuessNum.setOnItemClickListener(listenerNum);
		gv_ic_lottery_football.setOnItemClickListener(listenerzq);
		gv_ic_lottery_basketball.setOnItemClickListener(listenerlq);
		
		gv_ic_lottery_football.setSelector(new ColorDrawable(Color.TRANSPARENT));
		gv_ic_lottery_basketball.setSelector(new ColorDrawable(Color.TRANSPARENT));
		gv_GuessNum.setSelector(new ColorDrawable(Color.TRANSPARENT));
	}
		OnItemClickListener listenerlbji = new OnItemClickListener() {
		
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
				if (position == 3) {
					Intent intent = new Intent(getActivity(), FollowActivity.class);
					startActivity(intent);
				}else {
					((MainActivity) getActivity()).toLive(position);
				}
				
				
			}
		};
		
		OnItemClickListener listenerzq = new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				refreType = true;
				
				 getBallData(getActivity(),position);
				
			}
		};
		
		OnItemClickListener listenerlq = new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				//App.lottery = listLottery.get(1);
				refreType = true;
				 getBasketball(getActivity(),position);
			}
		};
		
		
		OnItemClickListener listenerNum = new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				if (listLottery==null) {
					listLottery = getData();
				}
	
				
				switch (arg2) {
				case 0://广东11选5
					/*App.lottery = listLottery.get(3);
					if (App.lottery.getLotteryID().equals("78")) {
						intent = new Intent(context, Select_11X5Activity.class);
						startActivity(intent);
					}*/
					
					itemOnclick(3);
					break;
				case 1://十一运夺金
					App.lottery = listLottery.get(5);
					if (App.lottery.getLotteryID().equals("62")) {
												
						//Log.e("11X5", App.lottery.getIsSale());
						intent = new Intent(context, Select_11X5Activity.class);
						startActivity(intent);
					}
					//itemOnclick(5);
					//Intent intent1 = new Intent(context, Select_11X5Activity.class);
					//startActivity(intent1);
					break;
				case 2://重庆时时彩
					App.lottery = listLottery.get(2);
					if (App.lottery.getLotteryID().equals("28")) {

						
						Intent intent2 = new Intent(context, Select_SSCActivity.class);
						startActivity(intent2);
					}
					break;
					
				case 3://江苏快3
					App.lottery = listLottery.get(4);
					if (App.lottery.getLotteryID().equals("83")) {
						// if(App.lottery.getIsSale().equals("true")){
						Intent intent3 = new Intent(context, Select_k3Activity.class);
						startActivity(intent3);
					}
					break;
				case 4://大乐透
					/*App.lottery = listLottery.get(8);
					if (App.lottery.getLotteryID().equals("39")) {
						intent = new Intent(context, Buy_DLT_Activit.class);
						startActivity(intent);
					}*/
					itemOnclick(8);
					
					break;
				case 5://胜负彩
					App.lottery = listLottery.get(6);
					if (App.lottery.getLotteryID().equals("74")) {
						
						intent = new Intent(context, Buy_SFC_Activity.class);// 胜负彩
						startActivity(intent);
					} 
					break;
				case 6://任九场
					App.lottery = listLottery.get(7);
					if (App.lottery.getLotteryID().equals("75")) {
						//Log.e("RX9", App.lottery.getIsSale());
						
						intent = new Intent(context, Buy_RX9_Activit.class);
						
						startActivity(intent);
					}
					break;
				default:
					break;
				}
				
			}
		};
		
	

	/**
	 * 自动登录
	 */
	public void doAutoLogin() {
		RequestUtil requestUtil = new RequestUtil(context, false, 0) {
			@Override
			public void responseCallback(JSONObject reponseJson) {
				
				String retult = AppTools.doLogin(reponseJson);
				
				if ("0".equals(retult)) {
					SharedPreferences settings = context.getSharedPreferences(
							"app_user", 0);
					boolean isLogin = false;
					String pass = "";
					if (settings.contains("isLogin")) {
						isLogin = settings.getBoolean("isLogin", false);
					}
					if (isLogin) {
						// 判断是否有存 密码
						if (settings.contains("pass")) {
							pass = settings.getString("pass", null);
						}
					}
					if (null != App.user)
						App.user.setUserPass(pass);
					if (RequestUtil.DEBUG)
						Log.i(TAG, "自动登录成功...");
					MyPushTask.newInstances(getActivity()).commit();
				} else {
					if (RequestUtil.DEBUG)
						Log.i(TAG, "自动登录失败" + retult);
				}
			}

			@Override
			public void responseError(VolleyError error) {
				if (RequestUtil.DEBUG)
					Log.i(TAG, "自动登录失败" + error.getMessage());
			}
		};
		requestUtil.doAutoLogin();
		requestUtils.add(requestUtil);
	}

	/**
	 * 得到竞彩 篮球数据
	 */
	public void getBasketball(final Context context,final int position) {
		RequestUtil requestUtil = new RequestUtil(context, true,
				Request.CONFIG_CACHE_MEDIUM, true, "正在加载对阵...") {
			@Override
			public void responseCallback(JSONObject object2) {
				try {
					if (null != object2) {
						Log.i(TAG, "竞彩篮球的数据" + object2);
						String error2 = object2.optString("error");
						if ("0".equals(error2)) {
							String detail2 = object2.optString("dtMatch");
							String detail_singlepass2 = object2
									.optString("Singlepass");
							if (detail2.length() == 0) {
								Log.i("x", "无数据");
							} else {
								// 拿到对阵信息组
								JSONArray array2 = new JSONArray(new JSONArray(detail2).toString());

								List<List<DtMatch_Basketball>> listMatch = new ArrayList<List<DtMatch_Basketball>>();
								for (int i = 0; i < array2.length(); i++) {
									JSONObject ob = array2.getJSONObject(i);
									String arr_4 = ob.optString("table4");
									String arr_l = ob.optString("table1");
									String arr2_l = ob.optString("table2");
									String arr3_l = ob.optString("table3");

									// 判断对阵 是否有

									listMatch.add(setList2(arr_l));
									listMatch.add(setList2(arr2_l));
									listMatch.add(setList2(arr3_l));
									listMatch.add(setList2(arr_4));
								}

								for (int i = 0; i < listLottery.size(); i++) {
									if ("73".equals(listLottery.get(i)
											.getLotteryID())) {
										JSONArray array_single2 = new JSONArray(
												new JSONArray(
														detail_singlepass2)
														.toString());

										List<List<DtMatch_Basketball>> listMatch_single = new ArrayList<List<DtMatch_Basketball>>();
										for (int j = 0; j < array_single2
												.length(); j++) {
											JSONObject ob = array_single2
													.getJSONObject(j);
											String arr_4 = ob
													.optString("table4");
											String arr_l = ob
													.optString("table1");
											String arr2_l = ob
													.optString("table2");
											String arr3_l = ob
													.optString("table3");

											// 判断对阵 是否有
											listMatch_single
													.add(setList2(arr_l));
											listMatch_single
													.add(setList2(arr2_l));
											listMatch_single
													.add(setList2(arr3_l));
											listMatch_single
													.add(setList2(arr_4));
										}
										App.DtMatch_Basketball = listMatch;
										App.DtMatch_Basketball_single = listMatch_single;
										App.lottery = listLottery.get(i);
										listLottery.get(i).setExplanation(object2.optString("explanation"));
									}
								}
							}

							if (!checkBasketBallDtMatch_IsEmpty(
									App.DtMatch_Basketball,
									App.DtMatch_Basketball_single)) {
								intent = new Intent(context,
										Select_jclqActivity.class);
								intent.putExtra("position", position);
								context.startActivity(intent);
							} else
								MyToast.getToast(context, "暂无篮球对阵信息...").show();
							
							
						}
					} else {
						if (RequestUtil.DEBUG)
							Log.i(TAG, "拿竞彩篮球数据为空");
						MyToast.getToast(context, "抱歉,服务器异常,请稍后重试...").show();
					}
				} catch (Exception e) {
					e.printStackTrace();
					if (RequestUtil.DEBUG)
						Log.i(TAG, "对阵获取失败，" + e.getMessage());
					MyToast.getToast(context, "抱歉,数据获取错误...").show();
				}
			}

			@Override
			public void responseError(VolleyError error) {
				if (RequestUtil.DEBUG)
					Log.e(TAG, "对阵获取失败，" + error.getMessage());
				MyToast.getToast(context, "抱歉,服务器异常,请稍后重试...").show();
			}
		};
		requestUtil.getJCData("73");
	}

	/**
	 * 判断篮球对阵是否为空
	 * 
	 * @param data
	 *            对阵信息
	 * @param data_single
	 *            单关对阵信息
	 * @return 判断结果 true为空 false不为空
	 */
	public boolean checkBasketBallDtMatch_IsEmpty(
			List<List<DtMatch_Basketball>> data,
			List<List<DtMatch_Basketball>> data_single) {
		boolean isEmpty = true;
		if (0 != data.size()) {
			for (int i = 0; i < data.size(); i++) {
				if (0 != data.get(i).size()) {
					isEmpty = false;
				}
			}
		}
		if (0 != data_single.size()) {
			for (int i = 0; i < data_single.size(); i++) {
				if (0 != data_single.get(i).size()) {
					isEmpty = false;
				}
			}
		}
		return isEmpty;
	}

	/**
	 * 判断足球对阵是否为空
	 * 
	 * @param data
	 *            对阵信息
	 * @param data_single
	 *            单关对阵信息
	 * @return 判断结果 true为空 false不为空
	 */
	public boolean checkFootBallDtMatch_IsEmpty(List<List<DtMatch>> data,
			List<List<DtMatch>> data_single) {
		boolean isEmpty = true;
		if (0 != data.size()||data!=null) {
			for (int i = 0; i < data.size(); i++) {
				if (0 != data.get(i).size()) {
					isEmpty = false;
				}
			}
		}

		if (0 != data_single.size()||data_single!=null) {
			for (int i = 0; i < data_single.size(); i++) {
				if (0 != data_single.get(i).size()) {
					isEmpty = false;
				}
			}
		}
		return isEmpty;
	}

	/**
	 * 篮球绑定数据 *
	 */
	private List<DtMatch_Basketball> setList2(String arr) {
		List<DtMatch_Basketball> list_m = new ArrayList<DtMatch_Basketball>();
		if (arr.length() > 5) {
			JSONArray Arr;
			try {
				Arr = new JSONArray(arr);
				DtMatch_Basketball dtmatch = null;
				for (int j = 0; j < Arr.length(); j++) {
					JSONObject item = Arr.getJSONObject(j);
					dtmatch = new DtMatch_Basketball();
					dtmatch.setMatchId(item.optString("matchId"));
					dtmatch.setMatchNumber(item.optString("matchNumber"));
					dtmatch.setMatchDate(item.optString("matchDate"));
					dtmatch.setGame(item.getString("game"));
					dtmatch.setGuestTeam(item.optString("guestTeam"));// 客队
					dtmatch.setMainTeam(item.getString("mainTeam"));// 主队
					dtmatch.setStopSellTime(item.optString("stopSellTime"));
					dtmatch.setDay(item.optString("day"));
					dtmatch.setMatchDate1(item.optString("matchDate1"));
					dtmatch.setMainLose(item.getString("mainLose"));
					dtmatch.setMainWin(item.getString("mainWin"));
					dtmatch.setSmall(item.getString("small"));
					dtmatch.setBigSmallScore(item.getString("bigSmallScore"));
					dtmatch.setBig(item.getString("big"));
					dtmatch.setLetScore(item.getString("letScore"));
					dtmatch.setLetMainLose(item.getString("letMainLose"));
					dtmatch.setLetMainWin(item.getString("letMainWin"));
					dtmatch.setMatchDate2(item.getString("matchWeek"));
					dtmatch.setDifferGuest1_5(item.optString("differGuest1_5"));
					dtmatch.setDifferGuest6_10(item
							.optString("differGuest6_10"));
					dtmatch.setDifferGuest11_15(item
							.optString("differGuest11_15"));
					dtmatch.setDifferGuest16_20(item
							.optString("differGuest16_20"));
					dtmatch.setDifferGuest21_25(item
							.optString("differGuest21_25"));
					dtmatch.setDifferGuest26(item.optString("differGuest26"));
					dtmatch.setDifferMain1_5(item.optString("differMain1_5"));
					dtmatch.setDifferMain6_10(item.optString("differMain6_10"));
					dtmatch.setDifferMain11_15(item
							.optString("differMain11_15"));
					dtmatch.setDifferMain16_20(item
							.optString("differMain16_20"));
					dtmatch.setDifferMain21_25(item
							.optString("differMain21_25"));
					dtmatch.setDifferMain26(item.optString("differMain26"));
					dtmatch.setSF(Boolean.parseBoolean(item.getString("isSF")
							.toLowerCase()));
					dtmatch.setDXF(Boolean.parseBoolean(item.getString("isDXF")
							.toLowerCase()));
					dtmatch.setRFSF(Boolean.parseBoolean(item.getString(
							"isRFSF").toLowerCase()));
					dtmatch.setSFC(Boolean.parseBoolean(item.getString("isSFC")
							.toLowerCase()));
					list_m.add(dtmatch);
				}
			} catch (Exception e) {
				e.printStackTrace();
				Log.i("x", "HallFragment错误" + e.getMessage());
			}
		}
		return list_m;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		/** 刷新 **/
		case 1:
			// getData();
			update();
			System.out
					.println("=================================在刷新方法里执行getData方法");
			break;
		/** 设置 **/
		case 2:
			intent = new Intent(context, SettingActivity.class);
			context.startActivity(intent);
			break;
		/** 更改账户 **/
		case 3:
			intent = new Intent(context, LoginActivity.class);
			intent.putExtra("loginType", "genggai");
			context.startActivity(intent);
			break;
		/** 退出 **/
		case 4:
			for (Activity activity : App.activityS) {
				activity.finish();
			}
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		menu.clear();
		int sysVersion = Integer.parseInt(VERSION.SDK);
		if (sysVersion > 10) {
			menu.add(1, 1, 0, "刷新");
			menu.add(1, 2, 0, "设置");
			menu.add(1, 3, 0, "更换账户");
			menu.add(1, 4, 0, "退出");
		} else {
			menu.add(1, 1, 0, "").setIcon(R.drawable.menu_refresh_select);
			menu.add(1, 2, 0, "").setIcon(R.drawable.menu_setting_select);
			menu.add(1, 3, 0, "").setIcon(R.drawable.menu_changeuser_select);
			menu.add(1, 4, 0, "").setIcon(R.drawable.menu_exit_select);
		}
		super.onCreateOptionsMenu(menu, inflater);
	}

	/**
	 * listView Item 的点击监听
	 */
	class ItemsClickListener implements OnItemClickListener {
		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int position,
				long arg3) {
			if (0 == listLottery.size() % 2) {
				itemOnclick(position);
			} else {
				if (listLottery.size() != position) {
					itemOnclick(position);
				}
			}
		}
	}

	private void itemOnclick(int position) {
		/*
		 * if (App.allLotteryName.get("更多内容").equals("100")) {
		 * MyToast.getToast(context, "该奖期已结束").show();
		 * 
		 * }
		 */

		// Log.i(TAG,"123123"+ App.allLotteryLogo.get());
		long currentTime = System.currentTimeMillis();

		if (listLottery.size() > 0) {
			
			App.lottery = listLottery.get(position); // 对应id
			if (App.lottery.getIsuseId() != null
					&& App.lottery.getIsuseId() != ""
					&& !"72".equals(App.lottery.getLotteryID())
					&& !"73".equals(App.lottery.getLotteryID())
					&& !"45".equals(App.lottery.getLotteryID())) {
				// if (!"72".equals(App.lottery.getLotteryID())
				// && !"73".equals(App.lottery.getLotteryID())
				// && !"45".equals(App.lottery.getLotteryID())) {

				if (App.lottery.getLotteryID().equals("6")) {
					// if(App.lottery.getIsSale().equals("true")){
					intent = new Intent(context, SelectNumberActivityFC3D.class);
					// }else{
					// Toast.makeText(context, "暂停开售",
					// Toast.LENGTH_SHORT).show();
					// return;
					// }
				} else if (App.lottery.getLotteryID().equals("63")) {
					// if(App.lottery.getIsSale().equals("true")){

					intent = new Intent(context, SelectNumberActivityPL3.class);
					// }else{
					// // Toast.makeText(context, "暂停开售",
					// Toast.LENGTH_SHORT).show();
					// return;
					// }
				} else if (App.lottery.getLotteryID().equals("3")
						|| App.lottery.getLotteryID().equals("64")) {
					// if(App.lottery.getIsSale().equals("true")){
					intent = new Intent(context,
							SelectNumberActivityPL5_QXC.class);
					// }else{
					// Toast.makeText(context, "暂停开售",
					// Toast.LENGTH_SHORT).show();
					// return;
					// }
				} else if (App.lottery.getLotteryID().equals("5")) {
					Log.e("SSQ", App.lottery.getIsSale());
					// if(App.lottery.getIsSale().equals("true")){
					intent = new Intent(context, SelectNumberActivity.class);
					// }else{
					// // Toast.makeText(context, "暂停开售",
					// Toast.LENGTH_SHORT).show();
					// return;
					// }
				} else if (App.lottery.getLotteryID().equals("39")) {
					// if(App.lottery.getIsSale().equals("true")){
					Log.e("DLT", App.lottery.getIsSale());
					intent = new Intent(context, Buy_DLT_Activit.class);
					// }else{
					// // Toast.makeText(context, "暂停开售",
					// Toast.LENGTH_SHORT).show();
					// return;
					// }
				} else if (App.lottery.getLotteryID().equals("74")) {
					// if(App.lottery.getIsSale().equals("true")){
					intent = new Intent(context, Buy_SFC_Activity.class);// 胜负彩
					// }else{
					// Toast.makeText(context, "暂停开售",
					// Toast.LENGTH_SHORT).show();
					// return;
					// }
				} else if (App.lottery.getLotteryID().equals("75")) {
					Log.e("RX9", App.lottery.getIsSale());

					// if(App.lottery.getIsSale().equals("true")){
					intent = new Intent(context, Buy_RX9_Activit.class);
					// }else{
					// // Toast.makeText(context, "暂停开售",
					// Toast.LENGTH_SHORT).show();
					// return;
					// }

				} else if (App.lottery.getLotteryID().equals("13")) {
					// if(App.lottery.getIsSale().equals("true")){
					intent = new Intent(context, Select_QlcActivity.class);
					// }else{
					// // Toast.makeText(context, "暂停开售",
					// Toast.LENGTH_SHORT).show();
					// return;
					// }
				} else if (App.lottery.getLotteryID().equals("62")
						|| App.lottery.getLotteryID().equals("70")
						|| App.lottery.getLotteryID().equals("78")) {
					// if(App.lottery.getIsSale().equals("true")){
					Log.e("11X5", App.lottery.getIsSale());
					intent = new Intent(context, Select_11X5Activity.class);
					// }else{
					// // Toast.makeText(context, "暂停开售",
					// Toast.LENGTH_SHORT).show();
					// return;
					// }
				} else if (App.lottery.getLotteryID().equals("28")) {

					Log.e("SSC", App.lottery.getIsSale());
					intent = new Intent(context, Select_SSCActivity.class);

				} else if (App.lottery.getLotteryID().equals("61")) {
					MyToast.getToast(context, "此彩种暂停销售！").show();
					return;

				} else if (App.lottery.getLotteryID().equals("83")) {
					// if(App.lottery.getIsSale().equals("true")){
					intent = new Intent(context, Select_k3Activity.class);
					// }else{
					// // Toast.makeText(context, "暂停开售",
					// Toast.LENGTH_SHORT).show();
					// return;
					// }
				} else {
					MyToast.getToast(context, "正在开发").show();
					return;
				}
				context.startActivity(intent);
			} else {
				refreType = true;
				if ("72".equals(App.lottery.getLotteryID())) {
					//getBallData(getActivity());
				} else if ("73".equals(App.lottery.getLotteryID())) {
					//getBasketball(getActivity());
				} else if ("45".equals(App.lottery.getLotteryID())) {
					getBjdcData(getActivity());
				}
				// }else{
				// MyToast.getToast(context,"暂无对阵信息").show();
				// }
			}
			if (!"73".equals(listLottery.get(position).getLotteryID())
					&& !"45".equals(listLottery.get(position).getLotteryID())
					&& !"72".equals(listLottery.get(position).getLotteryID())) {
				if (listLottery.get(position).getDistanceTime() - currentTime <= 0) {

					MyToast.getToast(context, "该奖期已结束，请等下一期").show();
					return;
				}

			}
		}
	}

	/**
	 * 得到竞彩 北单数据
	 */
	public void getBjdcData(final Context context) {
		if (refreType) {
			RequestUtil requestUtil = new RequestUtil(context, false, // false
																		// 不需缓冲使用
					Request.CONFIG_CACHE_MEDIUM, true, "正在加载对阵...") {
				@Override
				public void responseCallback(JSONObject object) {
					try {
						if (null != object) {
							String error = object.optString("error");
							if ("0".equals(error)) {
								String detail = object.optString("dtMatch");
								if (detail.length() == 0) {
									Log.i("x", "无数据");
								} else {
									// 拿到对阵信息组
									JSONArray array = new JSONArray(
											new JSONArray(detail).toString());

									List<List<DtMatchBJDC>> listMatchbjdc = new ArrayList<List<DtMatchBJDC>>();

									for (int i = 0; i < array.length(); i++) {
										JSONObject ob = array.getJSONObject(i);

										String arr = ob.optString("table1");
										String arr2 = ob.optString("table2");
										String arr3 = ob.optString("table3");

										listMatchbjdc.add(setListbjdc(arr));
										listMatchbjdc.add(setListbjdc(arr2));
										listMatchbjdc.add(setListbjdc(arr3));
									}
									for (int i = 0; i < listLottery.size(); i++) {
										if ("45".equals(listLottery.get(i)
												.getLotteryID())) {

											App.list_Matchs_bjdc = listMatchbjdc;
											App.lottery = listLottery
													.get(i);

											listLottery.get(i)
													.setList_Matchs_bjdc(
															listMatchbjdc);
											listLottery
													.get(i)
													.setExplanation(
															object.optString("explanation"));
										}
									}
								}
								if (!checkFootBjdc_IsEmpty(App.list_Matchs_bjdc)) {
									intent = new Intent(context,
											Select_bjdcActivity.class);
									context.startActivity(intent);
								} else
									MyToast.getToast(context, "暂无北单对阵信息...")
											.show();
							}
						} else {
							MyToast.getToast(context, "抱歉,服务器异常,请稍后重试...")
									.show();
							if (RequestUtil.DEBUG)
								Log.i(TAG, "拿北单数据为空");
						}
					} catch (Exception e) {
						e.printStackTrace();
						MyToast.getToast(context, "抱歉,数据获取错误...").show();
						if (RequestUtil.DEBUG)
							Log.i(TAG, "对阵获取失败，" + e.getMessage());
					}
				}

				@Override
				public void responseError(VolleyError error) {
					MyToast.getToast(context, "抱歉,服务器异常,请稍后重试...").show();
					if (RequestUtil.DEBUG)
						Log.i(TAG, "对阵获取失败，" + error.getMessage());
				}
			};
			requestUtil.getJCData("45");
		}
	}

	public boolean checkFootBjdc_IsEmpty(List<List<DtMatchBJDC>> data) {
		boolean isEmpty = true;
		if (0 != data.size()) {
			for (int i = 0; i < data.size(); i++) {
				if (0 != data.get(i).size()) {
					isEmpty = false;
				}
			}
		}

		return isEmpty;
	}

	/**
	 * 将对阵信息绑定 *
	 */
	@SuppressLint("DefaultLocale")
	private List<DtMatchBJDC> setListbjdc(String arr) {
		List<DtMatchBJDC> list_m = new ArrayList<DtMatchBJDC>();
		if (arr.length() > 6) {
			JSONArray Arr;
			try {
				Arr = new JSONArray(arr);
				DtMatchBJDC dtmatch = null;
				for (int j = 0; j < Arr.length(); j++) {
					JSONObject item = Arr.getJSONObject(j);
					dtmatch = new DtMatchBJDC();
					dtmatch.setMatchId(item.optString("matchId"));
					dtmatch.setMatchNumber(item.optString("matchNumber"));
					dtmatch.setGame(item.getString("game"));
					dtmatch.setGuestTeam(item.optString("guestTeam"));
					dtmatch.setMainTeam(item.getString("mainTeam"));
					// dtmatch.setSfloseball(item.getString("sflose"));//
					// 胜负过关的让球数
					dtmatch.setStopSellTime(item.optString("stopSellTime"));
					// dtmatch.setTitle(item.optString("title"));
					dtmatch.setTitle(item.optString("day"));
					// System.out.println("=====title======="+item.optString("title"));
					System.out.println("=====day======="
							+ item.optString("day"));
					// dtmatch.setMainLoseBall(item.optInt("mainLoseBall"));
					dtmatch.setMainLoseBall(item.optInt("giveBall"));
					dtmatch.setMatchWeek(item.optString("matchWeek"));
					/** 胜平负 */
					dtmatch.setSpfwin(item.optString("winSp"));
					dtmatch.setSpfflat(item.optString("flatSp"));
					dtmatch.setSpflose(item.optString("loseSp"));

					/** 上下单双 */
					dtmatch.setUpdan(item.optString("shangDanSP"));
					dtmatch.setUpshuang(item.optString("shangShuangSP"));
					dtmatch.setDowndan(item.optString("xiaDanSP"));
					dtmatch.setDownshuang(item.optString("xiaShuangSP"));

					/** 胜负 */
					// dtmatch.setSfggwin(item.optString("sfs"));
					// dtmatch.setSfgglose(item.optString("sff"));

					/** 猜比分 **/
					dtmatch.setSother(item.getString("bfWinSqtSp"));
					dtmatch.setS10(item.optString("bfWin10Sp"));
					dtmatch.setS20(item.optString("bfWin20Sp"));
					dtmatch.setS21(item.optString("bfWin21Sp"));
					dtmatch.setS30(item.optString("bfWin30Sp"));
					dtmatch.setS31(item.optString("bfWin31Sp"));
					dtmatch.setS32(item.optString("bfWin32Sp"));
					dtmatch.setS40(item.optString("bfWin40Sp"));
					dtmatch.setS41(item.optString("bfWin41Sp"));
					dtmatch.setS42(item.optString("bfWin42Sp"));
					// dtmatch.setS50(item.optString("s50"));
					// dtmatch.setS51(item.optString("s51"));
					// dtmatch.setS52(item.optString("s52"));

					dtmatch.setPother(item.getString("bfFlatPqtSp"));
					dtmatch.setP00(item.getString("bfFlat00Sp"));
					dtmatch.setP11(item.getString("bfFlat11Sp"));
					dtmatch.setP22(item.getString("bfFlat22Sp"));
					dtmatch.setP33(item.getString("bfFlat33Sp"));

					dtmatch.setFother(item.getString("bfLoseFqtSp"));
					dtmatch.setF01(item.getString("bfLose01Sp"));
					dtmatch.setF02(item.getString("bfLose02Sp"));
					dtmatch.setF12(item.getString("bfLose12Sp"));
					dtmatch.setF03(item.getString("bfLose03Sp"));
					dtmatch.setF13(item.getString("bfLose13Sp"));
					dtmatch.setF23(item.getString("bfLose23Sp"));
					dtmatch.setF04(item.getString("bfLose04Sp"));
					dtmatch.setF14(item.getString("bfLose14Sp"));
					dtmatch.setF24(item.getString("bfLose24Sp"));
					// dtmatch.setF05(item.getString("f05"));
					// dtmatch.setF15(item.getString("f15"));
					// dtmatch.setF25(item.getString("f25"));

					/** 半全场 **/
					dtmatch.setSs(item.getString("bqcSsSp"));
					dtmatch.setSp(item.getString("bqcSpSp"));
					dtmatch.setSf(item.getString("bqcSfSp"));
					dtmatch.setPs(item.getString("bqcPsSp"));
					dtmatch.setPp(item.getString("bqcPpSp"));
					dtmatch.setPf(item.getString("bqcPfSp"));
					dtmatch.setFs(item.getString("bqcFsSp"));
					dtmatch.setFp(item.getString("bqcFpSp"));
					dtmatch.setFf(item.getString("bqcFfSp"));

					/** 总进球 **/
					dtmatch.setIn0(item.getString("zjq0"));
					dtmatch.setIn1(item.getString("zjq1"));
					dtmatch.setIn2(item.getString("zjq2"));
					dtmatch.setIn3(item.getString("zjq3"));
					dtmatch.setIn4(item.getString("zjq4"));
					dtmatch.setIn5(item.getString("zjq5"));
					dtmatch.setIn6(item.getString("zjq6"));
					dtmatch.setIn7(item.getString("zjq7"));

					dtmatch.setSFGG(true);
					dtmatch.setSXDS(true);
					dtmatch.setCBF(true);
					dtmatch.setZJQ(true);
					dtmatch.setNewSPF(true);
					dtmatch.setBQC(true);
					list_m.add(dtmatch);
				}
			} catch (Exception e) {
				e.printStackTrace();
				Log.i("x", "错误" + e.getMessage());
			}
		}
		return list_m;
	}

	/**
	 * 获得所有彩种名称 以及 彩种ID
	 */
	private List<Lottery> getData() {
		System.out
				.println("======================执行getData方法======================");
		if (listLottery == null)
			listLottery = new ArrayList<Lottery>();
		if (refreType) {
			// 清空数据
			listLottery.clear();
			Lottery lottery = null;
			Iterator iterator = null;
			if (null != App.allLotteryName) {
				iterator = App.allLotteryName.entrySet().iterator();
			}
			if (null != iterator)
				while (iterator.hasNext()) {
					Map.Entry entry = (Map.Entry) iterator.next();
					String key = entry.getKey().toString();
					String value = entry.getValue().toString();
					lottery = new Lottery();
					lottery.setLotteryID(value);
					lottery.setLotteryName(key);
					listLottery.add(lottery);
				}
		}
		return listLottery;
	}

	/**
	 * 后台获得所有cp信息
	 */
	public void getLotteryData() {
		if (listLottery == null)
			listLottery = new ArrayList<Lottery>();

		if (refreType) {
			
			RequestUtil requestUtil = new RequestUtil(context, false, 0) {
				@Override
				public void responseCallback(JSONObject reponseJson) {
					Log.e("后台所有彩排信息", reponseJson.toString());
					String result = AppTools.getDate(reponseJson);
					if ("0".equals(result)) {
						update();
						/** 是否第一次进入程序 不是的话就自动登录 */
						if (App.index <= 1) {
							App.isShow = false;
							if (MainActivity.ll_main.getVisibility() == View.VISIBLE) {
								MainActivity.ll_main
										.setAnimation(AnimationUtils
												.loadAnimation(context,
														android.R.anim.fade_out));
								MainActivity.ll_main.setVisibility(View.GONE);
							}
							App.index++;
							if (MainActivity.isWin) {
								activity.toFindWininfo();
							}
						}
					} else if ("-1001".equals(result)) {
						if (RequestUtil.DEBUG) {
							Log.e(TAG, "获取购彩大厅数据出错");
						}
						update();
						/** 是否第一次进入程序 不是的话就自动登录 */
						if (App.index <= 1) {
							App.isShow = false;
							// 让其在显示的时候才会出现动画
							if (MainActivity.ll_main.getVisibility() == View.VISIBLE) {
								MainActivity.ll_main
										.setAnimation(AnimationUtils
												.loadAnimation(context,
														android.R.anim.fade_out));
								MainActivity.ll_main.setVisibility(View.GONE);
							}
							App.index++;
							if (MainActivity.isWin) {
								activity.toFindWininfo();
							}
						}
						MyToast.getToast(context, "数据获取失败,请手动刷新").show();
					}
				}

				@Override
				public void responseError(VolleyError error) {
					Log.e(TAG, "出错了," + error.getMessage());
					update();
					/** 是否第一次进入程序 不是的话就自动登录 */
					if (App.index <= 1) {
						App.isShow = false;
						if (MainActivity.ll_main.getVisibility() == View.VISIBLE) {
							((View) (MainActivity.ll_main))
									.setAnimation(AnimationUtils.loadAnimation(
											context, android.R.anim.fade_out));
							MainActivity.ll_main.setVisibility(View.GONE);
						}
						App.index++;
						if (MainActivity.isWin) {
							activity.toFindWininfo();
						}
					}
					MyToast.getToast(context, "数据获取失败,请手动刷新").show();
				}
			};
			requestUtil.getLotteryData(App.lotteryIds);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		 case R.id.iv_icon_service:
			 Intent intent = new Intent(getActivity(), CustomerActivity.class);
				startActivity(intent);
		 break;
		 case R.id.ll_zq_lottery_football:
			 //App.lottery = listLottery.get(0);
			 refreType = true;
			 getBallData(getActivity(),0);
			 break;
		 case R.id.ll_lq_lottery_basketball:
			 //App.lottery = listLottery.get(1);
			 refreType = true;
			 getBasketball(getActivity(),0);
			 break;

		}
	}


	/**
	 * 跳到幸运数字页面
	 */
	private void ToLuckActivity() {
		intent = new Intent(context, LuckyCenterActivity.class);
		context.startActivity(intent);
	}

	/**
	 * 得到竞彩 足球 数据
	 */
	public void getBallData(final Context context,final int position) {
		if (refreType) {
			
			RequestUtil requestUtil = new RequestUtil(context, true,
					Request.CONFIG_CACHE_MEDIUM, true, "正在加载对阵...\n"
							+ "对阵较多时加载会较慢...") {
				@Override
				public void responseCallback(JSONObject object) {
					try {
						if (null != object) {
							Log.i(TAG, "得到竞彩足球的数据" + object);
							String error = object.optString("error");
							Log.e("BaseBall", error);
							if ("0".equals(error)) {
								String detail = object.optString("dtMatch");
								String detail_singlepass = object
										.optString("Singlepass");
								if (detail.length() == 0) {
									Log.i("x", "无数据");
								} else {

									// 拿到对阵信息组
									JSONArray array = new JSONArray(
											new JSONArray(detail).toString());
									List<List<DtMatch>> listMatch = new ArrayList<List<DtMatch>>();
									for (int i = 0; i < array.length(); i++) {
										JSONObject ob = array.getJSONObject(i);
										String arr4 = ob.optString("table4");
										String arr = ob.optString("table1");
										String arr2 = ob.optString("table2");
										String arr3 = ob.optString("table3");

										// 判断对阵 是否有

										listMatch.add(setList(arr));
										listMatch.add(setList(arr2));
										listMatch.add(setList(arr3));
										listMatch.add(setList(arr4));

									}
									for (int i = 0; i < listLottery.size(); i++) {
										if ("72".equals(listLottery.get(i)
												.getLotteryID())) {
											// 拿到单关对阵信息组
											JSONArray array_singlepass = new JSONArray(
													new JSONArray(
															detail_singlepass)
															.toString());
											List<List<DtMatch>> listMatch_singlepass = new ArrayList<List<DtMatch>>();
											for (int j = 0; j < array_singlepass
													.length(); j++) {
												JSONObject ob = array_singlepass
														.getJSONObject(j);
												String arr4 = ob
														.optString("table4");
												if (!"".equals(arr4)) {// 如果第一个不是空的
													listMatch_singlepass
															.add(setList(arr4));
												}

												String arr = ob
														.optString("table1");
												if (!"".equals(arr)) {// 如果第一个不是空的
													listMatch_singlepass
															.add(setList(arr));
												}
												String arr2 = ob
														.optString("table2");
												if (!"".equals(arr2)) {// 如果第二个不是空的
													listMatch_singlepass
															.add(setList(arr2));
												}
												String arr3 = ob
														.optString("table3");
												if (!"".equals(arr3)) {// 如果第三个不是空的
													listMatch_singlepass
															.add(setList(arr3));
												}
											}
											App.list_singlepass_Matchs = listMatch_singlepass;
											App.list_Matchs = listMatch;
											listLottery
													.get(i)
													.setExplanation(
															object.optString("explanation"));
											App.lottery = listLottery.get(i);
											System.out
													.println("+++++++++++++++listMatch_singlepass"
															+ listMatch_singlepass
																	.size());
										}
									}
								}
								
								if (!checkFootBallDtMatch_IsEmpty(
										App.list_Matchs,
										App.list_singlepass_Matchs)) {
									
									intent = new Intent(context,
											Select_jczqActivity.class);
									intent.putExtra("isEmpty", false);
									intent.putExtra("position", position);
									context.startActivity(intent);
								} else
									MyToast.getToast(context, "暂无足球对阵信息...")
											.show();
							}
						} else {
							MyToast.getToast(context, "抱歉,服务器异常,请稍后重试...")
									.show();
							if (RequestUtil.DEBUG)
								Log.i(TAG, "拿竞彩足球数据为空");
						}
					} catch (Exception e) {
						e.printStackTrace();
						MyToast.getToast(context, "抱歉,数据获取错误...").show();
						if (RequestUtil.DEBUG)
							Log.i(TAG, "对阵获取失败，" + e.getMessage());
					}
				}

				@Override
				public void responseError(VolleyError error) {
					MyToast.getToast(context, "抱歉,服务器异常,请稍后重试...").show();
					// if(RequestUtil.DEBUG)
					Log.e(TAG, "对阵获取失败，" + error.getMessage());
				}
			};
			requestUtil.getJCData("72");
		}
	}

	/**
	 * 将对阵信息绑定 *
	 */
	@SuppressLint("DefaultLocale")
	private List<DtMatch> setList(String arr) {
		List<DtMatch> list_m = new ArrayList<DtMatch>();
		if (arr.length() > 5) {
			JSONArray Arr;
			try {
				Arr = new JSONArray(arr);
				DtMatch dtmatch = null;
				for (int j = 0; j < Arr.length(); j++) {
					JSONObject item = Arr.getJSONObject(j);
					dtmatch = new DtMatch();
					dtmatch.setMatchId(item.optString("matchId"));
					dtmatch.setMatchNumber(item.optString("matchNumber"));
					dtmatch.setGame(item.getString("game"));
					dtmatch.setGuestTeam(item.optString("guestTeam"));
					dtmatch.setMainTeam(item.getString("mainTeam"));
					dtmatch.setStopSellTime(item.optString("stopSellTime"));
					dtmatch.setDay(item.optString("day"));
					dtmatch.setMainLoseBall(item.optInt("mainLoseBall"));
					dtmatch.setMatchWeek(item.optString("matchWeek"));
					/** 胜平负 */
					dtmatch.setSpfwin(item.optString("spfwin"));
					dtmatch.setSpfflat(item.optString("spfflat"));
					dtmatch.setSpflose(item.optString("spflose"));
					/** 让球胜平负 */
					dtmatch.setWin(item.optString("win"));
					dtmatch.setFlat(item.optString("flat"));
					dtmatch.setLose(item.optString("lose"));
					dtmatch.setMatchDate(item.optString("matchDate"));
					/** 猜比分 **/
					dtmatch.setSother(item.getString("sother"));
					dtmatch.setS10(item.optString("s10"));
					dtmatch.setS20(item.optString("s20"));
					dtmatch.setS21(item.optString("s21"));
					dtmatch.setS30(item.optString("s30"));
					dtmatch.setS31(item.optString("s31"));
					dtmatch.setS32(item.optString("s32"));
					dtmatch.setS40(item.optString("s40"));
					dtmatch.setS41(item.optString("s41"));
					dtmatch.setS42(item.optString("s42"));
					dtmatch.setS50(item.optString("s50"));
					dtmatch.setS51(item.optString("s51"));
					dtmatch.setS52(item.optString("s52"));
					dtmatch.setPother(item.getString("pother"));
					dtmatch.setP00(item.getString("p00"));
					dtmatch.setP11(item.getString("p11"));
					dtmatch.setP22(item.getString("p22"));
					dtmatch.setP33(item.getString("p33"));
					dtmatch.setFother(item.getString("fother"));
					dtmatch.setF01(item.getString("f01"));
					dtmatch.setF02(item.getString("f02"));
					dtmatch.setF12(item.getString("f12"));
					dtmatch.setF03(item.getString("f03"));
					dtmatch.setF13(item.getString("f13"));
					dtmatch.setF23(item.getString("f23"));
					dtmatch.setF04(item.getString("f04"));
					dtmatch.setF14(item.getString("f14"));
					dtmatch.setF24(item.getString("f24"));
					dtmatch.setF05(item.getString("f05"));
					dtmatch.setF15(item.getString("f15"));
					dtmatch.setF25(item.getString("f25"));

					/** 半全场 **/
					dtmatch.setSs(item.getString("ss"));
					dtmatch.setSp(item.getString("sp"));
					dtmatch.setSf(item.getString("sf"));
					dtmatch.setPs(item.getString("ps"));
					dtmatch.setPp(item.getString("pp"));
					dtmatch.setPf(item.getString("pf"));
					dtmatch.setFs(item.getString("fs"));
					dtmatch.setFp(item.getString("fp"));
					dtmatch.setFf(item.getString("ff"));

					/** 总进球 **/
					dtmatch.setIn0(item.getString("in0"));
					dtmatch.setIn1(item.getString("in1"));
					dtmatch.setIn2(item.getString("in2"));
					dtmatch.setIn3(item.getString("in3"));
					dtmatch.setIn4(item.getString("in4"));
					dtmatch.setIn5(item.getString("in5"));
					dtmatch.setIn6(item.getString("in6"));
					dtmatch.setIn7(item.getString("in7"));

					dtmatch.setSPF(Boolean.parseBoolean(item.getString("isSPF")
							.toLowerCase()));
					dtmatch.setCBF(Boolean.parseBoolean(item.getString("isCBF")
							.toLowerCase()));
					dtmatch.setZJQ(Boolean.parseBoolean(item.getString("isZJQ")
							.toLowerCase()));
					dtmatch.setNewSPF(Boolean.parseBoolean(item.getString(
							"isNewSPF").toLowerCase()));
					dtmatch.setBQC(Boolean.parseBoolean(item.getString("isBQC")
							.toLowerCase()));

					list_m.add(dtmatch);
				}
			} catch (Exception e) {
				e.printStackTrace();
				Log.i("x", "错误" + e.getMessage());
			}
		}
		return list_m;

	}

	public void update() {
		
	}

	@Override
	public void onHeaderRefresh(PullToRefreshView view) {
		mPullToRefreshView.postDelayed(new Runnable() {
			@Override
			public void run() {
				if (NetWork.isConnect(context)) {
					refreType = true;
					getLotteryData();
					LoadImageview();
				} else {
					MyToast.getToast(context, "网络连接异常，请检查网络").show();
				}
				mPullToRefreshView.onHeaderRefreshComplete();
			}
		}, 1500);
	}

	/**
	 * 取消所有请求
	 */
	private void cancelAll() {
		if (null != requestUtils && 0 != requestUtils.size()) {
			int requestCount = requestUtils.size();
			for (int i = 0; i < requestCount; i++) {
				RequestUtil requestUtil = requestUtils.get(i);
				if (null != requestUtil) {
					Request request = requestUtil.getRequest();
					if (null != request)
						request.cancel();
				}
			}
		}
	}

	@Override
	public void onResume() {
		Log.i(TAG, "onResume");
		super.onResume();
		
		update();
	}

	@Override
	public void onStop() {
		super.onStop();
		Log.i(TAG, "onStop");
		cancelAll();
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.i(TAG, "onDestroy");
		ViewGroup parent = (ViewGroup) v.getParent();
		if (parent != null) {
			parent.removeAllViews();
		}

	}

	@Override
	public void onDestroyView() {
		Log.i(TAG, "onDestroyView");
		super.onDestroyView();
	}

	class MyAsynTask3 extends AsyncTask<Integer, Integer, String> {
		String error = "0";

		@Override
		protected String doInBackground(Integer... params) {
			String key = MD5.md5(App.MD5_key);

			if (time == null) {
				time = RspBodyBaseBean.getTime();
			}
			if (imei == null) {
				imei = RspBodyBaseBean.getIMEI(context);
			}
			info = "";
			crc = RspBodyBaseBean.getCrc(time, imei, key, info, "-1");
			auth = RspBodyBaseBean.getAuth(crc, time, imei, "-1");
			String[] values = { "73", auth, info };
			String result = HttpUtils.doPost(App.names, values,
					App.path);
			if ("-500".equals(result))
				return "-500";

			try {
				Log.i("图片获取-----------result", result);
				JSONObject JSONObject = new JSONObject(result);
				error = JSONObject.optString("error");
				String msgstr = JSONObject.optString("msg");
				Message msg = handler.obtainMessage();
				if ("0".equals(error)) {
					imageviews = (ArrayList<ImageViews>) JSON.parseArray(
							JSONObject.getJSONArray("picList").toString(),
							ImageViews.class);
					msg.what = 0;
					handler.sendMessage(msg);
				} else {
					msg.what = 2;
					msg.obj = msgstr;
					handler.sendMessage(msg);
				}

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();

			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			// handler.sendEmptyMessage(Integer.parseInt(result));
		}

	}

	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			switch (msg.what) {
			case 0:
				loopView.setAdapter(new LocalAdp(context, imageviews));
				loopView.setBoundaryCaching(true);
				loopView.setAutoScrollDurationFactor(10d);
				loopView.setInterval(3000);
				loopView.startAutoScroll();
				indy.setViewPager(loopView);
				indy.requestLayout();
				break;
			case 2:
				if (msg.obj != null) {
					MyToast.getToast(context, String.valueOf(msg.obj));
				}
				break;
			default:
				break;
			}

		}
	};

	private static class LocalAdp extends PagerAdapter {
		private int count = 100;
		private Queue<View> views;
		private List<ImageViews> data;
		private LayoutInflater lay;
		private Context context;

		LocalAdp(Context ct, List<ImageViews> listNews) {
			views = new LinkedList<View>();
			data = listNews;
			lay = LayoutInflater.from(ct);
			context = ct;
		}

		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(ViewGroup container, final int position) {
			View mage = views.poll();

			if (mage == null) {
				mage = lay.inflate(R.layout.item_image, null);
				mage.setId(count++);
			}
			ImageView iv = (ImageView) mage.findViewById(R.id.mage);
			iv.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					Intent intent = new Intent();
					intent.setAction("android.intent.action.VIEW");
				
					// TODO Auto-generated method stub
					if (position==0) {
//						Toast.makeText(context, "0", Toast.LENGTH_SHORT).show();
						Uri content_url = Uri.parse("http://gundong.haoqiucai.com/HD2/JIAJIANG.HTML");
						intent.setData(content_url);
						context.startActivity(intent);
					}else if (position==1) {
//						Toast.makeText(context, "1", Toast.LENGTH_SHORT).show();
						Uri content_url = Uri.parse("http://gundong.haoqiucai.com/HD1/songcaijin.HTML");
						intent.setData(content_url);
						context.startActivity(intent);
					}
				}
			});
			/*
			 * FIT_CONTER 安比例显示在中间
			 */
			iv.setScaleType(ScaleType.FIT_XY);

			ImageManager.getInstance().displayImage(
					data.get(position).getPicUrl(), iv,
					ImageManager.getViewsHeadOptions());

			container.addView(mage);
			return mage;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			View mage = (View) object;
			views.add(mage);
			Log.d("** des", mage.getId() + "  id" + " size " + views.size());
			container.removeView(mage);
		}

	}
}
